home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part2 / 13626 < prev    next >
Encoding:
Text File  |  1996-08-05  |  2.4 KB  |  83 lines

  1. Newsgroups: comp.lang.c++
  2. Path: in2.uu.net!shore!mv!usenet
  3. From: ENGR@GSSI.MV.COM (Michael Furman)
  4. Subject: Re: Advanced C++ question...
  5. Message-ID: <DovzxE.B0E@mv.mv.com>
  6. Mime-Version: 1.0
  7. Content-Type: Text/Plain; charset=US-ASCII
  8. Organization: GSSI
  9. Date: Tue, 26 Mar 1996 18:12:50 GMT
  10. References: <4iprfg$1ui@aadt> <DoMH88.5wv@mv.mv.com> <4j0u62$6na@newshost.centrum.is>
  11. X-Newsreader: WinVN 0.99.7
  12. X-Nntp-Posting-Host: gssi.mv.com
  13.  
  14. In article <4j0u62$6na@newshost.centrum.is>, bjarnir@centrum.is says...
  15. >
  16. >In article <DoMH88.5wv@mv.mv.com>, ENGR@GSSI.MV.COM (Michael Furman) says:
  17. >>   
  18. >>In article <4iprfg$1ui@aadt>, david_hooker@sdt.com says...
  19. >>>
  20. >>>Hi all...
  21. >>>
  22. >>>I want to write an operator* for a class, but I want to do
  23. >>>one of 2 things:
  24. >>>   1. Call one of two different operator*'s depending on whether
  25. >>>      it is an lvalue or rvalue
  26. >>>   OR
  27. >>>   2. Somehow determine in the function if it is being used as an
  28. >>>      lvalue or an rvalue.
  29. >>>   [......]
  30. >>>The reason for this is that this class accesses data differently 
  31. >>depending
  32. >>>on if it is a read or write access.
  33. >>
  34. >>It is impossible with "*" operator. But you can define conversion 
  35. >>functions
  36. >>for your class - it will allow exactly what you need.
  37. >>
  38. >
  39. >Could you explain this a bit further.  I have a similar problem to solve.
  40.  
  41. You can define a special class for using instead if embedded type (pseude_int
  42. instead of int in my example below). Define conversion to value (int) for
  43. reading it and "operator =" for writing:
  44.  
  45.  
  46. #include <stdio.h>
  47.  
  48. class pseudo_int
  49.   {        
  50. public:  
  51.   operator int()
  52.     {
  53.     printf("Value was read\n");
  54.     return 0;        // Or get / calculate any value and return it
  55.     }  
  56.   int operator = (const int & x)
  57.     {
  58.     printf("Value was assigned: %d\n", x);
  59.     // Do whatever you need with value x
  60.     return x;
  61.     }  
  62.   // Additional members / member functions if needed
  63.   } xxx;
  64.   
  65. int main()
  66.   {
  67.   int i;
  68.   i = xxx;
  69.   xxx = 5;  
  70.   return 0;
  71.   }    
  72.  
  73.  
  74. -- 
  75. <<< If you received it by E-mail: it is a copy of post to the newsgroup >>>
  76. ---------------------------------------------------------------
  77. Michael Furman,                       (603)893-1109
  78. Geophysical Survey Systems, Inc.  fax:(603)889-3984
  79. 13 Klein Drive - P.O. Box 97          engr@gssi.mv.com 
  80. North Salem, NH 03073-0097            71543.1334@compuserve.com
  81. ---------------------------------------------------------------
  82.  
  83.